package org.example.repository;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;

@Slf4j
public class TodoInitializer implements InitializingBean {

    private final TodoRepository repository;

    public TodoInitializer(TodoRepository repository) {
        this.repository = repository;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        initTestData();
    }

    private void initTestData() {
        log.info("init data");
        repository.clear();

        // insert by non-ordered
        repository.add(newTodo(1));
        repository.add(newTodo(3));
        repository.add(newTodo(2));
        repository.add(newTodo(5));
        repository.add(newTodo(4));
    }

    private Todo newTodo(int id) {
        return new Todo(id, "task " + id);
    }
}
